package com.vue.admin.controller;

import com.vue.admin.entity.SysUserRole;
import com.vue.admin.service.SysRoleService;
import com.vue.admin.service.SysUserRoleService;
import com.vue.admin.util.ResponseState;
import com.vue.admin.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping(value = "/sysUserRole")
public class SysUserRoleController {

    @Autowired
    private SysUserRoleService sysUserRoleServieImpl;
    @Autowired
    private SysRoleService sysRoleServiceImpl;

    @RequestMapping(value = "/selectByUserId")
    public ResponseState selectRoleByUserId(@RequestBody Map map) throws Exception {
        String userId = (String) map.get("userId");
        if (StringUtil.isEmpty(userId)) {
            return new ResponseState(false, "请选择用户！");
        }
        return new ResponseState(sysRoleServiceImpl.selectRoleByUserId(userId));
    }

    @RequestMapping(value = "/save")
    public ResponseState selectRoleByUserId(@RequestBody SysUserRole sysUserRole) throws Exception {
        if (StringUtil.isEmpty(sysUserRole.getUserId())) {
            return new ResponseState(false, "请选择用户！");
        }
        if (StringUtil.isEmpty(sysUserRole.getRoleId())) {
            return new ResponseState(false, "请选择角色！");
        }
        return new ResponseState(sysUserRoleServieImpl.save(sysUserRole));
    }

    @RequestMapping(value = "/delete")
    public ResponseState delete(@RequestBody SysUserRole sysUserRole) throws Exception {
        if (StringUtil.isEmpty(sysUserRole.getUserId())) {
            return new ResponseState(false, "请选择用户！");
        }
        if (StringUtil.isEmpty(sysUserRole.getRoleId())) {
            return new ResponseState(false, "请选择角色！");
        }
        return new ResponseState(sysUserRoleServieImpl.delete(sysUserRole));
    }

}
